/**
 * @Author: Bobby
 * @Date: 2021-12-30 10:01:42
 * @LastEditTime: 2022-03-16 17:14:59
 * @LastEditors: Bobby
 * @Description:
 * @FilePath: \wms-v2\src\utils\tools.js
 * @CIMC-V2
 */
// 不依赖基座 任何项目都可以调用
// import { CryptoJS } from './ase'
import { useStorage } from 'vue3-storage'
import CryptoJS from 'crypto-js'
const storage = useStorage(`CIMC-INOVIEW-${process.env.VUE_APP_BuildProject}-`)
//set
export function setStorage(key, value) {
  return storage.setStorageSync(key, value)
}
//get
export function getStorage(key) {
  return storage.getStorageSync(key)
}
//remove
export function removeStorage(key) {
  storage.removeStorageSync(key)
}
//clear
export function clearStorage() {
  storage.clearStorageSync()
}

//数组转Tree
export function arrayToTree(items, idName = 'ID', pidName = 'PID', childrenName = 'children') {
  let res = []
  let map = {}
  let getHasOwnProperty = (obj, property) => Object.prototype.hasOwnProperty.call(obj, property)

  for (const i of items) {
    map[i[idName]] = {
      ...i,
      [childrenName]: getHasOwnProperty(map, i[idName]) ? map[i[idName]][childrenName] : [],
    }
    const newItem = map[i[idName]]
    if (i[pidName] === 0 || i[pidName] === '') {
      res.push(newItem)
    } else {
      if (!getHasOwnProperty(map, i[pidName])) {
        map[i[pidName]] = {
          [childrenName]: [],
        }
      }
      map[i[pidName]][childrenName].push(newItem)
    }
  }
  return res
}

/*******
 * @description:更新url参数
 * @param {String} url
 * @param {String} param
 * @param {String} paramVal
 * @return {String}
 */
export function updateURLParameter(url, param, paramVal) {
  let newAdditionalURL = ''
  let tempArray = url.split('?')
  let baseURL = tempArray[0]
  let additionalURL = tempArray[1]
  let temp = ''
  if (additionalURL) {
    tempArray = additionalURL.split('&')
    for (let i = 0; i < tempArray.length; i++) {
      if (tempArray[i].split('=')[0] != param) {
        newAdditionalURL += temp + tempArray[i]
        temp = '&'
      }
    }
  }
  let rows_txt = temp + '' + param + '=' + paramVal
  return baseURL + '?' + newAdditionalURL + rows_txt
}

/*******
 * @description:解密数据
 * @param {String} data
 * @return {*}
 */
export function decryptd(data) {
  var key = data.slice(0, 2 << 2) + data.slice(-2 << 2)
  data = data.slice(2 << 2, -2 << 2)
  const keyHex = CryptoJS.enc.Utf8.parse(key)
  let encryptedHexStr = CryptoJS.enc.Base64.parse(data)
  let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr)
  // var key = data.slice(0, 2 << 2) + data.slice(-2 << 2)
  // data = data.slice(2 << 2, -2 << 2)
  // key = CryptoJS.enc.Utf8.parse(key)
  var decrypt = CryptoJS.AES.decrypt(srcs, keyHex, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7,
  })
  let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
  console.log(decryptedStr.toString())
  return decryptedStr.toString()
  // console.log(CryptoJS.enc.Utf8.stringify(decrypt).toString())
  // return CryptoJS.enc.Utf8.stringify(decrypt).toString()
}

function randomString(len) {
  len = len || 32
  var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
  var maxPos = $chars.length
  var pwd = ''
  for (let i = 0; i < len; i++) {
    pwd += $chars.charAt(Math.floor(Math.random() * maxPos))
  }
  return pwd
}
/*******
 * @description:加密数据
 * @param {String} data
 * @return {String}
 */
export function encryptd(data) {
  let key = randomString(2 << 3)
  var srcs = CryptoJS.enc.Utf8.parse(data)
  var encrypted = CryptoJS.AES.encrypt(srcs, CryptoJS.enc.Utf8.parse(key), {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7,
  })
  var resultData = key.slice(0, 2 << 2) + encrypted.toString() + key.slice(-2 << 2)
  return resultData
}

export function encryptd2(data) {
  let key = 'abc987aacki'
  var srcs = CryptoJS.enc.Utf8.parse(data)
  var encrypted = CryptoJS.AES.encrypt(srcs, CryptoJS.enc.Utf8.parse(key), {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7,
  })
  var resultData = key.slice(0, 2 << 2) + encrypted.toString() + key.slice(-2 << 2)
  return resultData
}

export function parseParams(url) {
  let temp1 = url.split('?')

  let pram = temp1[1]
  if (!pram) {
    return false
  }

  let keyValue = pram.split('&')
  let obj = {}
  for (let i = 0; i < keyValue.length; i++) {
    let item = keyValue[i].split('=')
    let key = item[0]
    let value = item[1]
    obj[key] = value
  }
  return obj
}

export function getUrlSystemApp() {
  let urlParams = parseParams(location.href)
  let _NowApp = urlParams.systemApp || 'def'
  if (_NowApp != 'def') {
    let key = _NowApp.split('#/')
    if (key.length > 0) {
      _NowApp = key[0]
    }
  }

  return _NowApp
}
